var _n2;
var cc__extends = __extends;
var cc__decorate = __decorate;
var cc__importDefault = __importDefault;
Object.defineProperty(exports, "__esModule", {
  value: true
});
var $1$Rope = require("Rope");
var s = cc__importDefault($1$Rope);
var cc__decorator = cc._decorator;
var ccp_ccclass = cc__decorator.ccclass;
var ccp_property = cc__decorator.property;
var def_Board = function (t) {
  function _ctor() {
    // 调用父类构造函数，如果有的话
    const instance = null !== t && t.apply(this, arguments) || this;

    // 初始化属性
    instance.boardSpr = null;        // 板子的精灵
    instance.holesNode = null;      // 孔的节点
    instance.objsNode = null;       // 对象节点
    instance.layerIndex = 1;        // 层级索引
    instance.isLocked = null;       // 是否锁定
    instance.boardColor = cc.Color.WHITE; // 板子颜色

    return instance; // 返回实例

  }
  cc__extends(_ctor, t);
  _ctor.prototype.onLoad = function () { };
  _ctor.prototype.start = function () {
    this.node.getComponent(cc.RigidBody).type = cc.RigidBodyType.Static;
    this.node.active = false;
  };
  _ctor.prototype.setBoardColor = function (t) {
    this.boardColor = t.clone();
    this.boardSpr.color = t;
  };
  _ctor.prototype.setLock = function (t, e) {
    // 设置透明度，如果透明度未定义，则默认为 255
    if (undefined === e) {
      e = 255;
    }
    this.node.opacity = e;

    // 获取节点的刚体组件
    var rigidBody = this.node.getComponent(cc.RigidBody);

    // 根据透明度设置节点状态和刚体类型
    if (e === 0) {
      this.node.active = false; // 隐藏节点
      if (rigidBody.type !== cc.RigidBodyType.Static) {
        this.scheduleOnce(() => {
          rigidBody.type = cc.RigidBodyType.Static; // 设置为静态
        });
      }
    } else {
      this.node.active = true; // 显示节点
      if (rigidBody.type !== cc.RigidBodyType.Dynamic) {
        this.scheduleOnce(() => {
          rigidBody.type = cc.RigidBodyType.Dynamic; // 设置为动态
        });
      }
    }

    // 检查锁定状态
    if (this.isLocked !== t) {
      this.isLocked = t;

      if (t) {
        // 如果锁定，设置颜色为灰色
        this.node.color = cc.Color.GRAY;
        this.boardSpr.color = cc.Color.GRAY;
        this.boardSpr.opacity = 10; // 最小透明度
      } else {
        // 如果解锁，恢复颜色并进行动画
        this.boardSpr.color = this.boardColor;
        let tintAction = cc.tintTo(0.3, 255, 255, 255);
        this.node.runAction(tintAction);
        this.boardSpr.opacity = 0;
        this.boardSpr.runAction(cc.fadeTo(0.5, 190));
      }

      // 更新所有螺丝组件的锁定状态
      var screwComps = this.getScrewComps();
      for (var index = 0; index < screwComps.length; index++) {
        screwComps[index].setLock(t);
      }
    }

  };
  _ctor.prototype.init = function (t) {
    // 按照 y 坐标对 holesNode 的子节点进行排序
    this.holesNode.children.sort((a, b) => a.y - b.y);

    // 遍历排序后的子节点，进行实例化和设置连接
    this.holesNode.children.forEach((holeNode) => {
      const instantiatedRope = cc.instantiate(t);
      instantiatedRope.position = holeNode.position; // 设定新对象的位置
      instantiatedRope.parent = this.objsNode; // 设置父节点

      const rigidBody = instantiatedRope.getComponent("Rope").sprite.getComponent(cc.RigidBody);

      const revoluteJoint = this.node.addComponent(cc.RevoluteJoint); // 添加旋转关节
      revoluteJoint.connectedBody = rigidBody; // 连接刚体
      revoluteJoint.anchor = cc.v2(instantiatedRope.position.x, instantiatedRope.position.y); // 设定连接点
      revoluteJoint.apply(); // 应用关节
    });

  };
  _ctor.prototype.getScrewComps = function () {
    // 从 objsNode 的子节点中提取指定组件
    const componentsArray = this.objsNode.children.map(child => {
      return child.getComponent(s.default);
    });

    // 返回组件数组
    return componentsArray;

  };
  cc__decorate([ccp_property(cc.Node)], _ctor.prototype, "boardSpr", undefined);
  cc__decorate([ccp_property(cc.Node)], _ctor.prototype, "holesNode", undefined);
  cc__decorate([ccp_property(cc.Node)], _ctor.prototype, "objsNode", undefined);
  cc__decorate([ccp_property(cc.Color)], _ctor.prototype, "boardColor", undefined);
  return cc__decorate([ccp_ccclass], _ctor);
}(cc.Component);
exports.default = def_Board;